using System;

using TrueCode.Core.Aspects;

namespace TrueCode.Core.Disposing
{
  public class DisposeAction : IDisposable
  {
    #region Fields
    private readonly Action _action;
    private bool _disposed;
    #endregion

    #region .ctors
    public DisposeAction( [NotNull] Action action )
    {
      if( action == null ) throw new ArgumentNullException( "action" );
      _action = action;
    }
    #endregion

    #region Implementation of IDisposable
    public void Dispose()
    {
      if( !_disposed )
      {
        _disposed = true;
        _action();
      }
    }
    #endregion
  }
}